package com.xike.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;

import org.apache.http.util.EncodingUtils;

import com.xike.net.NetConstant;

public class StringUtil {
	public static int toInt(String string, int defaultValue) {
		if (string == null || string.equals("")) {
			return defaultValue;
		}
		try {
			return Integer.parseInt(string);
		} catch (NumberFormatException err) {
			return defaultValue;
		}
	}

	public static float toFloat(String string, float defaultValue) {
		if (string == null || string.equals("")) {
			return defaultValue;
		}
		try {
			return Float.parseFloat(string);
		} catch (NumberFormatException err) {
			return defaultValue;
		}
	}

	public static long toLong(String string, long defaultValue) {
		if (string == null || string.equals("")) {
			return defaultValue;
		}
		try {
			return Long.parseLong(string);
		} catch (NumberFormatException err) {
			return defaultValue;
		}
	}

	public static boolean toBoolean(String string, boolean defaultValue) {
		if (string == null || string.equals("")) {
			return defaultValue;
		}
		try {
			return Boolean.parseBoolean(string);
		} catch (NumberFormatException err) {
			return defaultValue;
		}
	}

	public static String arrayToString(String[] strArray) {
		if (strArray == null || strArray.length == 0) {
			return "";
		}
		StringBuffer s = new StringBuffer();
		int len = strArray.length;
		for (int i = 0; i < len; i++) {
			s.append(strArray[i]);
			if (i >= 0 && i < len - 1) {
				s.append(',');
			}
		}
		return s.toString();
	}

	/**
	 * @param s
	 *            原有字符串
	 * @return MD5后的字符串
	 * @WZL @2012-3-26@上午11:39:53
	 * @description 将字符串md5
	 */
	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str).toUpperCase();
		} catch (Exception e) {
			return null;
		}
	}

	// /**
	// * @param string 原有字符串
	// * @param len 目标字符串的长度
	// * @return
	// * @WZL wzl200711402@163.com @2012-3-30@上午10:56:43
	// * @description 得到目标长度的字符串
	// */
	// public static String toSizeString(String string, int len) {
	// StringBuffer sb = new StringBuffer();
	// try {
	// int size = string.length();
	// if (len > size) {
	// sb.append(string);
	// } else {
	// sb.append(string.substring(0, len - 1)).append("...");
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return sb.toString();
	// }

	/**
	 * @param is
	 *            数据流
	 * @return
	 * @throws IOException
	 * @WZL wzl200711402@163.com @2012-5-10@上午09:40:00
	 * @description 将数据流转换为字符串
	 */
	public static String inputStream2String(InputStream is) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(is));
		StringBuffer buffer = new StringBuffer();
		String line = "";
		while ((line = in.readLine()) != null) {
			buffer.append(line);
		}
		return buffer.toString();
	}

	/**
	 * @param theString
	 *            html字符串
	 * @return
	 * @WZL wzl200711402@163.com @2012-5-16@上午11:03:32
	 * @description 将字符串中的非法字符转换
	 */
	public static String HtmlEncode(String theString) {
		theString = theString.replace("#", "%23");
		theString = theString.replace("%", "%25");
		theString = theString.replace("\\", "%27");
		theString = theString.replace("?", "%3f");
		theString = theString.replace(" ", "&nbsp;");
		return theString;
	}

	public static boolean toBooleanForTestMode(String string, boolean defaultValue) {
		if (string == null || string.equals("")) {
			return defaultValue;
		}
		try {
			if ("".equals("close")) {
				return false;
			} else {
				return true;
			}
		} catch (Exception ex) {
			return defaultValue;
		}
	}

	public static String fileToString(File file) {
		String text = "";
		try {
			FileInputStream fin = new FileInputStream(file);
			int length = fin.available();
			byte[] buffer = new byte[length];
			fin.read(buffer);
			text = EncodingUtils.getString(buffer, NetConstant.DEFAULT_CONTENT_CHARSET);
			fin.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return text;
	}

	/**
	 * @description:
	 * @user wzl email:wzl200711402@163.com
	 * @date 2013-11-12
	 * @param content
	 *            原字符串
	 * @param len
	 *            保留字个数
	 * @return
	 */
	public static String getLengthString(String content, int len) {
		if (content == null) {
			return "";
		}
		if (content.length() > len) {
			content = content.substring(0, len) + "...";
		}
		return content;
	}

	/**
	 * @description:去除字符中所有空格
	 * @user wzl email:wzl200711402@163.com
	 * @date 2014-2-13
	 * @param content
	 * @return
	 */
	public static String toTrim(String content) {
		if (content == null) {
			return null;
		}
		return content.trim().replaceAll(" ", "");
	}

	/**
	 * @description:判断字符串是否为空
	 * @user wzl email:wzl200711402@163.com
	 * @date 2014-2-13
	 * @param content
	 * @return
	 */
	public static boolean isNotNull(String content) {
		if (null == content || "".equals(content.trim())) {
			return false;
		}
		return true;
	}
}
